home *** CD-ROM | disk | FTP | other *** search
/ Disc to the Future 2 / Disc to the Future Part II Programmer's Reference (Wayzata Technology)(6013)(1992).bin / MAC / THINKC / TCL1 / GRAPH_FO / (GRAPH / GRAPH_SO / GREDGE.C < prev    next >
Text File  |  1991-06-13  |  5KB  |  234 lines

  1. /******************************************************************************
  2.     GrEdge.c    
  3.         Graph methods in Object C.
  4.         This implements the GrEdge class.
  5.         
  6.     SUPERCLASS = GrNode
  7.     
  8.     Copyright ⌐ 1991 Maarten Meijer. All rights reserved.
  9.         CIS 100016,1764; FidoNet 2:512/114
  10. *******************************************************************************/
  11.  
  12.  
  13. #include "GrEdge.h"
  14. #include <Global.h>
  15.  
  16. #define    DELTA            2        /* size of edge hotRegion */
  17. #define    INOUT            /* separate input from output edges */
  18. /* #define    DEBUGEDGE        /* draw edge hotRegion outlines */
  19. #define SELFRECT        20
  20.  
  21. /* GrEdge methods ****************************************************/
  22.  
  23. void
  24. GrEdge::IGraphNode() {
  25.     inherited::IGraphNode();
  26.     fromVertex = toVertex = (GrVertex *)NULL;
  27.     }
  28.  
  29. void
  30. GrEdge::_Draw() {
  31.     Point    whereFrom, whereTo;
  32.     Rect    r, fromRect, toRect;
  33.     
  34.     if((fromVertex != (GrVertex *)NULL) && (toVertex != (GrVertex *)NULL)) {
  35.         if(fromVertex == toVertex) {
  36.             whereFrom = fromVertex->center;
  37.             r.top = whereFrom.v;
  38.             r.left = whereFrom.h;
  39.             r.bottom = whereFrom.v + SELFRECT;
  40.             r.right = whereFrom.h + SELFRECT;
  41.             FrameOval(&r);
  42.             }
  43.         else {
  44.             whereFrom = fromVertex->center;
  45.             whereTo = toVertex->center;
  46. #ifdef    INOUT
  47.             fromVertex->GetRect(&fromRect);
  48.             toVertex->GetRect(&toRect);
  49.             MoveTo(fromRect.right, whereFrom.v);
  50.             LineTo(fromRect.right + 5, whereFrom.v);
  51.             LineTo(toRect.left - 5, whereTo.v);
  52.             LineTo(toRect.left, whereTo.v);
  53. #else
  54.             MoveTo(whereFrom.h, whereFrom.v);
  55.             LineTo(whereTo.h, whereTo.v);
  56. #endif
  57.             }
  58.         }
  59.     }
  60.  
  61. void
  62. GrEdge::Draw() {
  63.     _Draw();
  64.  
  65. /* this is now handled by the Draw(&area) method */
  66. /*
  67. #ifndef    INOUT
  68.     fromVertex->Draw();
  69.     toVertex->Draw();
  70. #endif
  71. */
  72.     }
  73.  
  74. void
  75. GrEdge::_Erase() {
  76. /*
  77.     PenState    ps;
  78.  
  79.     GetPenState(&ps);
  80.     PenPat(white);
  81.     _Draw();
  82.     SetPenState(&ps);
  83. #ifdef    DEBUGEDGE
  84.     if(hotRegion)
  85.         EraseRgn(hotRegion);
  86. #endif
  87. */
  88.     }
  89.  
  90. void
  91. GrEdge::Erase() {
  92. /*
  93.     _Erase();
  94.     fromVertex->Draw();
  95.     toVertex->Draw();
  96. */
  97.     }
  98.  
  99. void
  100. GrEdge::SetRegion() {
  101.     register Point    to, from;
  102.     register int    dh;
  103.     Point            temp;
  104.     Rect            toRect, fromRect;
  105.  
  106.     to = toVertex->center;
  107.     from = fromVertex->center;
  108.     
  109.     if( EqualPt(to, from) ) {
  110.         inherited::SetRegion();
  111.         OpenRgn();
  112.         _Draw();
  113.         CloseRgn(hotRegion);        
  114.         }
  115.     else {
  116. #ifdef    INOUT
  117.         fromVertex->GetRect(&fromRect);
  118.         toVertex->GetRect(&toRect);
  119.         to.h = toRect.left - 5;
  120.         from.h = fromRect.right + 5;
  121. #endif
  122.         temp.h = from.h - to.h;
  123.         temp.v = from.v - to.v;
  124.         if((temp.h > 0 && temp.v > 0) || (temp.h < 0 && temp.v < 0)) {
  125.             dh = -(DELTA);
  126.             }
  127.         else {
  128.             dh = DELTA;
  129.             }
  130.         inherited::SetRegion();
  131.         OpenRgn();
  132.         MoveTo(from.h + dh, from.v + DELTA);
  133.         LineTo(to.h + dh, to.v + DELTA);
  134.         LineTo(to.h - dh, to.v - DELTA);
  135.         LineTo(from.h - dh, from.v - DELTA);
  136.         LineTo(from.h + dh, from.v + DELTA);
  137.         CloseRgn(hotRegion);
  138. #ifdef    DEBUGEDGE
  139.             if(hotRegion != NULL)
  140.                 FrameRgn(hotRegion);
  141. #endif
  142.         }
  143.     }
  144.  
  145. Boolean
  146. GrEdge::Incident(GrNode *which) {
  147.     return((fromVertex == which) || (toVertex == which));
  148.     }
  149.  
  150. void
  151. GrEdge::SetFrom(GrVertex *which) {
  152.     if((fromVertex != (GrVertex *)NULL) && (toVertex != (GrVertex *)NULL)) {
  153.         Erase();
  154.         }
  155.     fromVertex = which;
  156.     if((fromVertex != (GrVertex *)NULL) && (toVertex != (GrVertex *)NULL)) {
  157.         Draw();
  158.         SetRegion();
  159.         }
  160.     }
  161.  
  162. GrVertex *
  163. GrEdge::GetFrom() {
  164.     return fromVertex;
  165.     }
  166.  
  167. void
  168. GrEdge::SetTo(GrVertex *which) {
  169.     if((fromVertex != (GrVertex *)NULL) && (toVertex != (GrVertex *)NULL)) {
  170.         Erase();
  171. /*        toVertex->Draw(); */    /* allready in Erase() */
  172.         }
  173.     toVertex = which;
  174.     if((fromVertex != (GrVertex *)NULL) && (toVertex != (GrVertex *)NULL)) {
  175.         Draw();
  176. /*        fromVertex->Draw(); */
  177. /*        toVertex->Draw(); */
  178.         SetRegion();
  179.         }
  180.     }
  181.  
  182. GrVertex *
  183. GrEdge::GetTo() {
  184.     return toVertex;
  185.     }
  186.  
  187. /******************************************************************************
  188.     GetRect
  189.     
  190.         return the rect that encloses this edge's fromVertex & toVertex
  191. *******************************************************************************/
  192. void
  193. GrEdge::GetRect(register Rect *rect) {
  194.     Point    pt1, pt2;
  195.     Rect    fromRect, toRect;
  196.  
  197.     pt1 = fromVertex->GetCenter();
  198.     pt2 = toVertex->GetCenter();
  199.     if( EqualPt(pt1, pt2) ) {        /* self ref */
  200.         pt2.h += SELFRECT;
  201.         pt2.v += SELFRECT;
  202.         Pt2Rect(pt1, pt2, rect);
  203.         }
  204.     else
  205.         {
  206.         Pt2Rect(pt1, pt2, rect);    
  207.         if(rect->top == rect->bottom)
  208.             InsetRect(rect, 0, -5);
  209.         if( rect->left == rect->right || pt1.h >= pt2.h) {
  210.             fromVertex->GetRect(&fromRect);
  211.             toVertex->GetRect(&toRect);
  212.             UnionRect(&fromRect, &toRect, rect);
  213.             InsetRect(rect, -15, 0);
  214.             }
  215.         rect->bottom += 1;
  216.         rect->right += 1;
  217.         }
  218.     }
  219.  
  220. /******************************************************************************
  221.     NodeInRect
  222.     
  223.         Determine if the edge is within a specified Rect, to see whether
  224.         needs to be redrawn. Should this methode use SectRect??
  225. *******************************************************************************/
  226. Boolean
  227. GrEdge::NodeInRect(Rect *r) {
  228.     Rect    area;
  229.  
  230.     GetRect(&area);
  231.     return SectRect(r, &area, &area);
  232. /*    return RectInRgn(r, hotRegion);*/
  233.     }
  234.